const BaseIpcHandler = require('../BaseIpcHandler');
const { RemoteScriptInvoker } = require('../../remote/remoteScriptInvoker');

class RemoteInstallHandler extends BaseIpcHandler {
    async handle(event, params) {
        // 解析参数
        // console.log(`>> RemoteInstallHandler params = `, params);
        const { remoteId } = params;

        // 业务逻辑
        let result = null;
        try {
            let invoker = new RemoteScriptInvoker(this.app, remoteId);
            await invoker.install();
            result = { code: 0, msg: `Install software OK!` };
        } catch (err) {
            console.error(`Install remote${remoteId} software Failed!, Error = `, err.msg);
            result = { code: -1, msg: err.msg };
        }

        // 返回结果
        if (event.from === 'localserver') {
            event.resp.send(result);
        } else {
            return result;
        }
    }
}

module.exports = RemoteInstallHandler;
